<template>
  <div class="favorites-page">
    <div class="favorites-header">
      <h2>收藏的岗位</h2>
    </div>
    
    <el-table :data="favoriteList" style="width: 100%">
      <el-table-column prop="company" label="公司名称" width="200">
        <template #default="scope">
          <div class="company-info">
            <img :src="scope.row.logo || defaultLogo" :alt="scope.row.company" class="company-logo">
            <span>{{ scope.row.company }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="岗位名称" width="200"></el-table-column>
      <el-table-column prop="location" label="工作地点" width="150"></el-table-column>
      <el-table-column prop="salary" label="薪资范围" width="150">
        <template #default="scope">
          <span class="salary-text">{{ scope.row.salary }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="tags" label="岗位标签" width="200">
        <template #default="scope">
          <el-tag v-for="tag in scope.row.tags" :key="tag" size="small" :color="getTagColor(tag)" style="margin-right: 5px;">
            {{ tag }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="收藏时间" width="180"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button size="small" type="warning" @click="removeFavorite(scope.row)">
            取消收藏
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <div class="no-data" v-if="favoriteList.length === 0">
      <el-empty description="暂无收藏的岗位" />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
// 导入收藏相关的API接口
import { getFavorites, removeFavorite } from '@/api/favorite'

const favoriteList = ref([])
const defaultLogo = ref('@/assets/images/default-company.png')

// 获取收藏列表
const getFavoriteList = async () => {
  try {
    const res = await getFavorites()
    favoriteList.value = res.data
  } catch (error) {
    console.error('获取收藏列表失败:', error)
    ElMessage.error('获取收藏列表失败: ' + (error.message || '未知错误'))
    // 使用模拟数据作为备选
    favoriteList.value = [
      {
        id: 1,
        job_id: 1,
        title: '高级前端工程师',
        company: '腾讯科技',
        salary: '20K-35K',
        location: '深圳',
        created_at: '2023-06-15 10:30:00',
        tags: ['全职', '远程', '弹性工作'],
        logo: '@/assets/images/tencent.png'
      },
      {
        id: 2,
        job_id: 2,
        title: 'Java架构师',
        company: '阿里巴巴',
        salary: '30K-50K',
        location: '杭州',
        created_at: '2023-06-10 14:20:00',
        tags: ['全职'],
        logo: '@/assets/images/alibaba.png'
      }
    ]
  }
}

// 取消收藏
const removeFavoriteFunc = async (row) => {
  try {
    await removeFavorite(row.job_id)
    ElMessage.success('取消收藏成功')
    // 重新获取列表
    await getFavoriteList()
  } catch (error) {
    console.error('取消收藏失败:', error)
    ElMessage.error('取消收藏失败: ' + (error.message || '未知错误'))
  }
}

const getTagColor = (tag) => {
  const colors = {
    '全职': '#409eff',
    '兼职': '#67c23a',
    '实习': '#e6a23c',
    '远程': '#909399',
    '弹性工作': '#6a5acd',
    '年终奖': '#ff6b6b'
  }
  return colors[tag] || '#c0c4cc'
}

onMounted(() => {
  getFavoriteList()
})
</script>

<style lang="scss" scoped>
.favorites-page {
  padding: 20px;
  background-color: #fff;
  
  .favorites-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    
    h2 {
      margin: 0;
      color: #303133;
    }
  }
  
  .company-info {
    display: flex;
    align-items: center;
    
    .company-logo {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      margin-right: 10px;
      object-fit: cover;
    }
  }
  
  .salary-text {
    font-weight: bold;
    color: #6a5acd;
  }
  
  .no-data {
    text-align: center;
    padding: 40px 0;
  }
}
</style>